package edu.core.testapp.client;

import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

public class StoredFileWidget extends Composite {
	
	public final AbsolutePanel mainPanel = new AbsolutePanel();
	
	public final HorizontalPanel panel = new HorizontalPanel();
	
	public final FlexTable fileTable = new FlexTable();
	
	public final Label error = new Label();
	
	public final StoredFileServiceAsync service = GWT.create(StoredFileService.class);

	public StoredFileWidget() {
		
		service.getStoredFiles(new AsyncCallback<List<String>>() {
			
			@Override
			public void onSuccess(List<String> result) {
				// TODO Auto-generated method stub
				fileTable.clear();
				
				for (int i = 0; i < result.size(); i++) {
					//fileTable.setText(i, 0, result.get(i));
					Anchor a = new Anchor(result.get(i));
					a.setHref("http://127.0.0.1:8888/core_testapp/fileServlet/file.txt?id=" + result.get(i));
					a.setTarget("_blank");
					
					Button x = new Button("X");
					x.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							// TODO Auto-generated method stub
							
						}
					});
					
					fileTable.setText(i, 0, "" + (i + 1));
					fileTable.setWidget(i, 1, a);
					fileTable.setWidget(i, 2, x);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				error.setText(caught.getMessage());
			}
		});
		
		//mainPanel.setSize("500px", "500px");
		//mainPanel.add(fileTable, 0, 50);
		
		//mainPanel.add(error, 0, 0);
		
		panel.add(fileTable);
		panel.add(error);
		
		//initWidget(mainPanel);
		initWidget(panel);
		
	}
	
}
